package com.umbrella.worker.web.controller.manager;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.umbrella.worker.dto.AdminDO;
import com.umbrella.worker.dto.OperateDO;
import com.umbrella.worker.dto.RegisterDO;
import com.umbrella.worker.dto.RoleDO;
import com.umbrella.worker.dto.SupplierDO;
import com.umbrella.worker.query.AdminQuery;
import com.umbrella.worker.result.ResultDO;
import com.umbrella.worker.result.ResultSupport;
import com.umbrella.worker.service.IAdminService;
import com.umbrella.worker.service.IMenuService;
import com.umbrella.worker.service.IRoleService;
import com.umbrella.worker.service.ISuppliersService;
import com.umbrella.worker.util.GetHttpMemberInfo;
import com.umbrella.worker.util.MD5;

@Controller
@RequestMapping(value = "/manager")
public class ManagerContrlloer {
	
	@Autowired
	private IMenuService menuService;
	
	@Autowired
	private IAdminService adminService;
	
	@Autowired
	private IRoleService roleService;
	
	@Autowired
	private ISuppliersService suppliersService;
	
	@RequestMapping(value = "/register.html", method = RequestMethod.GET)
	public ModelAndView register(ModelAndView mav, 
			HttpServletRequest request) {
		mav.setViewName("manager2/register");
		return mav;
	}
	
	@RequestMapping(value = "/addsupplier.html", method = RequestMethod.GET)
	public ModelAndView addsupplier(ModelAndView mav, 
			HttpServletRequest request) {
		mav.setViewName("manager2/supplier/add");
		return mav;
	}


	@RequestMapping(value = "/login.html", method = RequestMethod.GET)
	public ModelAndView login(ModelAndView mav, 
			HttpServletRequest request) {
		mav.setViewName("manager2/login");
		return mav;
	}
	
	@RequestMapping(value = "/login2.html", method = RequestMethod.GET)
	public ModelAndView adminLogin(ModelAndView mav, 
			HttpServletRequest request) {
		mav.setViewName("manager/login");
		return mav;
	}
	
	@RequestMapping(value = "/verityCode/{code}.json", method = RequestMethod.GET)
	public ModelAndView vertiyCode(ModelAndView mav,  
			@PathVariable(value="code") String code, 
			HttpServletRequest request) {
		String verityCode = (String) request.getSession().getAttribute("VERITY_CODE");
		if(verityCode.equals(code)) {
			mav.addObject("JSON_DATA", 1);
		} else {
			mav.addObject("JSON_DATA", 0);
		}
		return mav;
	}
	
	@RequestMapping(value = "/login2.html", method = RequestMethod.POST)
	public ModelAndView adminLogin(ModelAndView mav, 
			AdminDO adminDO, HttpServletRequest request) {
		
		String verityCode = (String) request.getSession().getAttribute("VERITY_CODE");
		
		if(!adminDO.getVcode().equals(verityCode)) {
			mav.setViewName("manager/login");
		}
		
		String loginIP = GetHttpMemberInfo.getIpAddr(request);
		adminDO.setwALoginIp(loginIP);
		
		MD5 md5 = new MD5();
		String md5Pwd = md5.getMD5ofStr(adminDO.getwAPassword() 
				+ adminDO.getwAUsername());
		
		adminDO.setwAPassword(md5Pwd);
		
		
		ResultDO result = adminService.validate2(adminDO);

		if(result.isSuccess()) {
			adminDO = (AdminDO) result.getModel(ResultSupport.FIRST_MODEL_KEY);
			request.getSession().setAttribute("MANAGER_ID", adminDO.getId());
			request.getSession().setAttribute("MANAGER_NAME", adminDO.getwAUsername());
			request.getSession().setAttribute("MANAGER_ROLE_ID", adminDO.getwARoleId());
			request.getSession().setAttribute("MANAGER_SUPPLIER_ID", adminDO.getwASupplierId());
			request.getSession().setAttribute("MANAGER_SUPPLIER_NAME", adminDO.getSupplierName());
			request.getSession().setAttribute("MANAGER_TELEPHONE", adminDO.getwATelephone());
			
			
			return new ModelAndView("redirect:/manager/index.html");
		} else {
			mav.setViewName("manager/login");
		}
		return mav;
	}
	
	@RequestMapping(value = "/check/{username}.json", method = RequestMethod.GET)
	public ModelAndView checkUsername(ModelAndView mav, 
			@PathVariable(value="username") String username,
			HttpServletRequest request) {
		AdminQuery query = new AdminQuery();
		query.setUsername(username);
		ResultDO result = adminService.list(query);
		List<AdminDO> list = null;
		if(result.isSuccess()) {
			list = (List<AdminDO>) result.getModel(ResultSupport.FIRST_MODEL_KEY);
			if(list.size() == 0) {
				return mav.addObject("JSON_DATA", 1);
			} else {
				return mav.addObject("JSON_DATA", 0);
			}
		} else {
			return mav.addObject("JSON_DATA", 0);
		}
	}
	
	@RequestMapping(value = "/register.html", method = RequestMethod.POST)
	public ModelAndView register(ModelAndView mav, 
			RegisterDO registerDO, HttpServletRequest request) {
		
		
		SupplierDO supplierDO = registerDO.getSupplierDO();
		
		AdminDO adminDO = registerDO.getAdminDO();
		
		supplierDO.setCreateAuthor(adminDO.getwAUsername());
		supplierDO.setModifiAuthor(supplierDO.getCreateAuthor());
		supplierDO.setwSLegal("1");
		supplierDO.setwSLegalId("1");
		supplierDO.setwSGrade(100);
		//上传图片保存的ID
		Integer attac=supplierDO.getwSAttacId();
		supplierDO.setwSAttacId(attac);
		System.out.println(attac+" /////////// attac");
		
		ResultDO result = suppliersService.create(supplierDO);
		
		if(!result.isSuccess()) {
			mav.setViewName("error");
			return mav;
		}
		Integer supplierId = (Integer) result.getModel(ResultSupport.FIRST_MODEL_KEY);
		if(supplierId==null){
			supplierId=supplierDO.getId();
		}
		adminDO.setwASupplierId(supplierId);
		MD5 md5 = new MD5();
		String md5Pwd = md5.getMD5ofStr(adminDO.getwAPassword() 
				+ adminDO.getwAUsername());
		adminDO.setwARealName(supplierDO.getwSContact());
		adminDO.setwAPassword(md5Pwd);
		adminDO.setwATelephone(registerDO.getSupplierDO().getwSTelephone());
		adminDO.setCreateAuthor(adminDO.getwAUsername());
		adminDO.setModifiAuthor(adminDO.getCreateAuthor());
		
		result = adminService.create(adminDO);
		
		if(result.isSuccess()) {
			if(supplierDO.getFlag()==1){
				mav.setViewName("manager2/supplier/review");
			}
			
			if(supplierDO.getFlag()==2){
				//跳转到待审核的页面
				return new ModelAndView("redirect:/supplier/list/review/1.html");
			}
			
		} else {
			mav.setViewName("error");
		}
		return mav;
	}
	
	@RequestMapping(value = "/login.html", method = RequestMethod.POST)
	public ModelAndView login(ModelAndView mav, 
			AdminDO adminDO, HttpServletRequest request) {
		
		String verityCode = (String) request.getSession().getAttribute("VERITY_CODE");
		
		if(!adminDO.getVcode().equals(verityCode)) {
			mav.setViewName("manager2/login");
		}
		
		String loginIP = GetHttpMemberInfo.getIpAddr(request);
		adminDO.setwALoginIp(loginIP);
		
		MD5 md5 = new MD5();
		String md5Pwd = md5.getMD5ofStr(adminDO.getwAPassword() 
				+ adminDO.getwAUsername());
		
		adminDO.setwAPassword(md5Pwd);
		
		
		ResultDO result = adminService.validate(adminDO);

		if(result.isSuccess()) {
			adminDO = (AdminDO) result.getModel(ResultSupport.FIRST_MODEL_KEY);
			request.getSession().setAttribute("MANAGER_ID", adminDO.getId());
			request.getSession().setAttribute("MANAGER_NAME", adminDO.getwAUsername());
			request.getSession().setAttribute("MANAGER_ROLE_ID", adminDO.getwARoleId());
			request.getSession().setAttribute("MANAGER_SUPPLIER_ID", adminDO.getwASupplierId());
			request.getSession().setAttribute("MANAGER_SUPPLIER_NAME", adminDO.getSupplierName());
			request.getSession().setAttribute("MANAGER_TELEPHONE", adminDO.getwATelephone());
			
			//判断渠道商的状态
			if(adminDO.getwASupplierId()!=null){
				ResultDO res =suppliersService.get(adminDO.getwASupplierId());
				SupplierDO supplierDO = (SupplierDO) res.getModel(ResultSupport.FIRST_MODEL_KEY);
				//状态码 0：停用 1：启用  2:待审核 3 审核不通过 4审核通过
				if(supplierDO.getStatus()==3){
					return new ModelAndView("manager2/supplier/failed");
				}
				if(supplierDO.getStatus()==2){
					return new ModelAndView("manager2/supplier/review");
				}
			}
			
			//跳转总后台 暂定
			if(adminDO.getwAUsername().equals("admin") || adminDO.getwARoleId()==1){
				//return new ModelAndView("redirect:/members/list/1.html");
				//跳转到后台登录
				return new ModelAndView("redirect:/manager/login2.html");
			}
			
			//跳转渠道商后台  暂定
			return new ModelAndView("redirect:/morder/waitOrderList.html");
			
		} else {
			mav.setViewName("manager2/login");
		}
		return mav;
	}
	
	@RequestMapping(value = "/loginout.html", method = RequestMethod.GET)
	public ModelAndView loginOut(ModelAndView mav, 
			HttpServletRequest request) {
		request.getSession().removeAttribute("MANAGER_ID");
		request.getSession().removeAttribute("MANAGER_NAME");
		request.getSession().removeAttribute("MANAGER_ROLE_ID");
		request.getSession().removeAttribute("MANAGER_SUPPLIER_ID");
		return new ModelAndView("redirect:/manager/login.html");
	}
	
	@RequestMapping(value = "/index.html", method = RequestMethod.GET)
	public ModelAndView index(ModelAndView mav, 
			HttpServletRequest request) {
		
		Integer roleId = (Integer) request.getSession().getAttribute("MANAGER_ROLE_ID");
		
		if(roleId == null) {
			return new ModelAndView("redirect:/manager/login2.html");
		}
		
		ResultDO result = roleService.get(roleId);
		
		if(result.isSuccess()) {
			RoleDO roleDO = (RoleDO) result.getModel(ResultSupport.FIRST_MODEL_KEY);
			List<OperateDO> menus = roleDO.getOperates();
			
			System.out.println(roleId+" ********** "+menus);
			request.getSession().setAttribute("MENU_LIST", menus);
			mav.setViewName("manager/index");
		} else {
			mav.setViewName("error");
		}
		mav.setViewName("manager/index");
		return mav;
	}
	

	
}
